package edu.uc.labs.MobilePrinting;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * A class that checks for and installs needed files for the MobilePrint Project
 * 
 * @author Matt Silbernagel
 */
public class Installer {

	public boolean installed = false;

	/*
	 * The directory structure should look like this: installDir/ cache/ icons/
	 * tray.png tray_mac.png logs/ ppd/ drivers/
	 */
	public int size = 6;
	public String[] dirsNeeded = new String[size];
	public String trayIcon = null;
	public Configurator conf = new Configurator();

	/**
	 * Set up the directory structure
	 */
	public Installer() {
		dirsNeeded[0] = Configurator.CACHEDIR; // installDir
		dirsNeeded[1] = dirsNeeded[0] + Configurator.CACHE; // cache
		dirsNeeded[2] = dirsNeeded[0] + Configurator.ICONS; // icons
		dirsNeeded[3] = dirsNeeded[0] + Configurator.LOGS;  // logs
		dirsNeeded[4] = dirsNeeded[0] + Configurator.PPD;   // ppd for *NIX
		dirsNeeded[5] = dirsNeeded[0] + Configurator.PREFS; // preferences 
		trayIcon = dirsNeeded[2] + Configurator.DELIM + Configurator.TRAYICON;
	}

	public void install() {

		for (int i = 0; i < size; i++) {
			File f = new File(dirsNeeded[i]);
			if (!f.exists()) {
				f.mkdirs();
			}
		}

		String imgName = Configurator.TRAYICON;
		File f = new File(dirsNeeded[2] + Configurator.DELIM + imgName);
		File f2 = new File(dirsNeeded[2] + Configurator.DELIM
				+ Configurator.TRAYICON_NIX);
		File f3 = new File(dirsNeeded[2] + Configurator.DELIM
				+ Configurator.TRAYICON_MAC);
		File f4 = new File(dirsNeeded[2] + Configurator.DELIM
				+ Configurator.DEFAULT_CONF);
		
		if (!f.exists()) {
			try {
				InputStream is = this.getClass().getResourceAsStream(
						"images" + Configurator.DELIM + Configurator.TRAYICON);
				OutputStream os = new FileOutputStream(dirsNeeded[2]
						+ Configurator.DELIM + Configurator.TRAYICON);
				byte[] buffer = new byte[10000];
				int length;
				while ((length = is.read(buffer)) > 0) {
					os.write(buffer, 0, length);
				}
				os.close();
				is.close();
			} catch (NullPointerException e) {
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}

		if (!f2.exists()) {
			try {
				InputStream is = this.getClass().getResourceAsStream(
						"images" + Configurator.DELIM
								+ Configurator.TRAYICON_NIX);
				OutputStream os = new FileOutputStream(dirsNeeded[2]
						+ Configurator.DELIM + Configurator.TRAYICON_NIX);
				byte[] buffer = new byte[10000];
				int length;
				while ((length = is.read(buffer)) > 0) {
					os.write(buffer, 0, length);
				}
				os.close();
				is.close();
			} catch (Exception e) {

			}
		}

		if (!f3.exists()) {
			try {
				InputStream is = this.getClass().getResourceAsStream(
						"images" + Configurator.DELIM
								+ Configurator.TRAYICON_MAC);
				OutputStream os = new FileOutputStream(dirsNeeded[2]
						+ Configurator.DELIM + Configurator.TRAYICON_MAC);
				byte[] buffer = new byte[10000];
				int length;
				while ((length = is.read(buffer)) > 0) {
					os.write(buffer, 0, length);
				}
				os.close();
				is.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		if (!f4.exists()) {
			try {
				InputStream is = this.getClass().getResourceAsStream(
						"Prefs" + Configurator.DELIM
								+ Configurator.DEFAULT_CONF);
				OutputStream os = new FileOutputStream(dirsNeeded[5]
						+ Configurator.DELIM + Configurator.DEFAULT_CONF);
				byte[] buffer = new byte[10000];
				int length;
				while ((length = is.read(buffer)) > 0) {
					os.write(buffer, 0, length);
				}
				os.close();
				is.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * Getter for installed
	 * @return true if all files are installed, false otherwise
	 */
	public boolean isInstalled() {
		return installed;
	}

}
